<title>InputEvent: macOS Yank</title>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<div id="editable" contenteditable></div>
<textarea id="txt"></textarea>
<script>
test(() => {
    assert_not_equals(window.eventSender, undefined, 'This test requires eventSender.');
    assert_not_equals(window.testRunner, undefined, 'This test requires testRunner.');

    let eventRecorder = '';
    document.addEventListener('beforeinput', event => {
        eventRecorder += `beforeinput-${event.inputType}-${event.data}-`;
    });
    document.addEventListener('input', event => {
        eventRecorder += `input-${event.inputType}-${event.data}-`;
    });

    const editable = document.getElementById('editable');
    editable.innerHTML = 'abc';
    editable.focus();
    const selection = window.getSelection();
    selection.collapse(editable, 1); // End of first line.

    // Delete a word to setup kill buffer.
    eventSender.keyDown('Backspace', ['altKey']);
    assert_equals(editable.innerHTML, '');

    // Test Yank.
    eventRecorder = '';
    testRunner.execCommand('yank');
    assert_equals(editable.innerHTML, 'abc');
    assert_equals(eventRecorder, 'beforeinput-insertFromYank-abc-input-insertFromYank-abc-');
}, 'Yank on contenteditable');

test(() => {
    assert_not_equals(window.eventSender, undefined, 'This test requires eventSender.');
    assert_not_equals(window.testRunner, undefined, 'This test requires testRunner.');

    let eventRecorder = '';
    document.addEventListener('beforeinput', event => {
        eventRecorder += `beforeinput-${event.inputType}-${event.data}-`;
    });
    document.addEventListener('input', event => {
        eventRecorder += `input-${event.inputType}-${event.data}-`;
    });

    const txt = document.getElementById('txt');
    txt.value = 'abc';
    txt.focus();
    txt.setSelectionRange(3, 3); // End of first line.

    // Delete a word to setup kill buffer.
    eventSender.keyDown('Backspace', ['altKey']);
    assert_equals(txt.value, '');

    // Test Yank.
    testRunner.execCommand('undo'); // Execute undo to close last typing command.
    eventRecorder = '';
    testRunner.execCommand('yank');
    assert_equals(txt.value, 'abc');
    assert_equals(eventRecorder, 'beforeinput-insertFromYank-abc-input-insertFromYank-abc-');
}, 'Yank on <textarea>');
</script>
